package com.myidis.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.myidis.entity.User;

public interface UserMapper {
	@Select("SELECT * FROM user")
    @Results({
        @Result(property = "group", column = "user_group")
    })
    List<User> all();
	
	@Select("SELECT * FROM user WHERE dept = #{dept} AND role = #{role} AND concat(userName, name, post, email, phone) like #{name}")
	List<User> getAllCon(User user);
	
	@Select("SELECT * FROM user WHERE concat(userName, name, post, email, phone) like #{name}")
	List<User> getByCon(User user);
	
	@Select("SELECT * FROM user WHERE dept = #{dept} AND concat(userName, name, post, email, phone) like #{name}")
	List<User> getByDept(User user);
	
	@Select("SELECT * FROM user WHERE role = #{role} AND concat(userName, name, post, email, phone) like #{name}")
	List<User> getByRole(User user);

    @Select("SELECT * FROM user WHERE id = #{id}")
    @Results({
        @Result(property = "group", column = "user_group")
    })
    User getOne(int id);
    
    @Select("SELECT * FROM user WHERE userName = #{userName}")
    @Results({
        @Result(property = "group", column = "user_group")
    })
    User getByUN(String userName);

    @Insert("INSERT INTO user(username, password, name, dept, role, user_group, post, email, phone) VALUES(#{userName}, #{password}, #{name}, #{dept}, #{role} , #{group}, #{post}, #{email}, #{phone})")
    int insert(User user);

    @Update("UPDATE user SET username = #{userName}, name = #{name}, dept = #{dept}, role = #{role}, user_group = #{group}, post = #{post}, email = #{email}, phone = #{phone} WHERE id = #{id}")
    int update(User user);
    
    @Update("UPDATE user SET password = #{password} WHERE id = #{id}")
    int pswUpdate(User user);

    @Delete("DELETE FROM user WHERE id = #{id}")
    int delete(int id);
}
